var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: "../../../images/2.jpg",
    wxOpenId: "",
    picId:"",
    companyName:"",
    companyAddress:"",
    telephoneNumber:"",
    belongIndustry:"",
    companyDesc:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.openId)
    this.setData({
      wxOpenId: options.openId
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },

  /**
   * 点击更换图片
   */
  updateimg: function () {
    var that = this;
    //选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        //正在上传提示
        wx.showLoading({
          title: '正在上传...',
        })
        //判断图片大小
        if (res.tempFiles[0].size <= 3145728) {
          //上传文件
          wx.uploadFile({
            url: app.globalData.serverAddress + '/api/company/upload?_t=' + new Date().getTime(),
            filePath: tempFilePaths[0],
            name: 'img',
            header: {
              "Content-Type": "multipart/form-data",
              'accept': 'application/json'
            },
            formData: {
              'user': 'test' //其他额外的formdata，可不写
            },
            success: function (res) {
              console.log(res.data);
              var result = JSON.parse(res.data);
              if (result.flag) {
                var picUrl = that.data.avatarUrl;
                var picId = that.data.picId;
                picUrl = result.url;
                picId = result.picId;
                that.setData({
                  avatarUrl: picUrl,
                  picId: picId
                });
              } else {
                console.log(result.msg);
              }
            },
            fail: function (res) {
              wx.showModal({
                content: '网络异常，图片上传失败',
                showCancel: false,
                success: function (res) { }
              })
            },
            complete: function () {
              wx.hideLoading();
            }
          })
        } else {
          //图片大于3M不予上传
          wx.showToast({
            title: '上传图片不能大于3M!',
            icon: 'none'
          })
        }
      },
      fail: e => {
        console.error(e)
      }
    })
  },

  //提交表单
  companysubmit: function (e) {
    var data = e.detail.value;
    var url = app.globalData.serverAddress + "/api/company/card/save?_t=" + new Date().getTime();
    app.httpPost(url, data).then((res) => {
      if (res.data.flag) {
        wx.switchTab({
          url: "/pages/my/my",
        })
        // var comp = JSON.stringify(res.data.company);
        // return comp;
      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg); //错误提示信息
      wx.showModal({
        content: '网络异常',
        showCancel: false,
      })
    });
  },
  //点击更多跳转
  more:function(){
    var data=this.data;
    console.log(data);
    var url = app.globalData.serverAddress + "/api/company/card/save?_t=" + new Date().getTime();
    app.httpPost(url, data).then((res) => {
      if (res.data.flag) {
        wx.navigateTo({
          url: "/pages/company/edit-card/edit-card?companyId=" + res.data.companyId + '&_t=' + new Date().getTime()
        })
      }else{
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg); //错误提示信息
      wx.showModal({
        content: '网络异常',
        showCancel: false,
      })
    });
    // wx.navigateTo({
    //   url: "/pages/company/edit-card/edit-card?companyId=9f3d762d2a3f4916b5646bc6d7c0b4de&_t="+new Date().getTime()
    // })
  },
  //input绑定输入的值事件
  inputValue: function(e){
    var name = e.currentTarget.dataset.name;
    this.setData({
      [name]:e.detail.value
    });
  }
})